<template>
  <div class="navbar-custom-menu">
    <!--用户信息-->
    <ul class="nav navbar-nav" id="navMain">
      <li class="dropdown messages-menu need-to-do">
        <a href="#" class="dropdown-toggle dbalertexpand" data-toggle="dropdown" aria-expanded='false' >
          <i class="fa"></i>
          <div class="message-reminding"></div>
          <div class="message-tip">待办</div>
        </a>
        <DropdownMenuCom :allMsg=buildMsgObj v-on:xxRedPoint="xxRedPoint">

        </DropdownMenuCom>
        <!-- <ul class="dropdown-menu">
          <div class="messages-menu-header"></div>
          <div class="dropdown-menu-body messages-menu-xx"></div>
        </ul> -->
      </li>
      <!-- Tasks: style can be found in dropdown.less -->
      <li class="dropdown messages-menu need-read">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded='false' >
          <i class="fa"></i>
          <div class="message-reminding"></div>
          <div class="message-tip">消息</div>
        </a>
        <DropdownMenuCom :allMsg=buildMsgObj v-on:dbRedPoint="dbRedPoint">

        </DropdownMenuCom>
            <!-- <ul class="dropdown-menu">
          <div class="tasks-menu-header messages-menu-header"></div>
          <div class="dropdown-menu-body db-menu-db"></div>
        </ul> -->
      </li>

      <li class="dept"><span id="deptName" :class="{'overFlow-text':overFlow}" :title="deptName">{{deptName}}</span> </li>
      <li class="divider vertical-divider"></li>
      <li @click="openSet" :class="{
      'dropdown':true,
       'user':true,
       'user-menu':true,
       'open':openF
      }">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" :aria-expanded="openF">
          <img src="/static/resources/img/user-pt.jpg" class="user-image" alt="User Image">
          <span id="username" class="user-name">{{empCode}}</span><i class="fa fa-chevron-up"></i><i class="fa fa-chevron-down"></i>
        </a>
        <ul class="dropdown-menu">
          <div class="dropdown-menu-body">
            <li class="user-menu" @click.stop="checkedTab(0)">
              <div role="menuitem"  data-toggle="modal" data-target="#sys-set-win">
                <a href="#"  data-toggle="tab">系统设置</a>
              </div>
            </li>
            <li class="user-menu" role="menuitem" data-toggle="modal" data-target="#switch-dept-win" @click="showswitchdept">切换组织</li>
            <li class="user-menu"><a href="http://foss.deppon.com/bse-baseinfo-web/login/oAPasswordAuthentication.action" target="_blank">修改密码</a></li>
            <li class="divider" role="presentation"></li>
            <li class="user-menu" role="menuitem" @click="logout">退出</li>
          </div>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script>
import store from '../../store/store';
import {mapActions,mapState} from 'vuex';
import DropdownMenuCom from './DropdownMenuCom';

    export default {
        name: "nav-custom-menu-com",
        store,
        data(){
          return {
            deptName:userInfo.optDeptName,
            empCode:userInfo.empCode,
            overFlow:false,
            buildMsgObj:[],    // 传递给父组件的消息和待办数组
            openF:false
          }
        },
        mounted(){
          this.overFlow = (document.getElementById('deptName').scrollHeight > document.getElementById('deptName').offsetHeight)
        },
        created(){
          const queryTreeMes=()=> {
            this.axios.post(MESSAGE_SERVICE_URL + "treeMessageController/queryTreeMessage",
              {key: "1"})
              .then((result)=>{
                let rs = result.data;
                // document.querySelector(".messages-menu-xx .1008").style.display = "none";
                // $(".need-to-do .messages-menu-header").html("<div>您有" + "<span></span>" + "新待办任务</div>");
                // $(".need-read .messages-menu-header").html("<div>您有" + "<span></span>" + "新消息任务</div>");
                this.queryFossMsg(rs);
                setTimeout(this.refreshRedPoint(),800);
              }).catch((httpRequest)=>{
                console.log('待办消息组件：',httpRequest);
              });
          }
          queryTreeMes();
          const bigcss = (rs,strygdsb)=>{
            var delength = document.querySelectorAll(".messages-menu-xx li").length;
            for (var i = 0; i < dblength; i++) {
            	if(document.qieruSelectorAll(".messages-menu-xx li")==undefined ||document.qieruSelectorAll(".messages-menu-xx li")=='' ){
            	}else{
                if(document.qieruSelectorAll(".messages-menu-xx li")[i] != undefined){
                  var dbcontent = document.qieruSelectorAll(".messages-menu-xx li")[i].className;
                  if (dbcontent != "1008") {
                      document.qieruSelectorAll(".messages-menu-xx li")[i].style.display = "none";
                  }
                }
            	}
            }
            document.querySelector(".db-menu-db li").style.display = "none";
            //消息循环遍历
          }
          // const supercss=function (res) {
          //   res = res[0];
          //   var lis = "<li class='cssappend'>"+
          //   "<a  class='dropdown-toggle' data-toggle='dropdown' aria-expanded='false' href='javascript:return false;' >"+
          //   "<i class='fa fa-envelope-o'></i>"+
          //   "<span onclick=\"viewTab(\'" + res.mesName + "\',\'" +
          //   res.linkUrl + "\',\'IFRAME\')\"  class='label label-success'>"
          //   + res.msgNumber + "</span>";
          //   $("#navMain").prepend(lis);
          // }
        },
        methods: {
          ...mapActions('departmentStore',['GET_currentDeptInfo']),
          logout(){
            this.axios.get(LOGSTATISTICS_SERVICE_URL + 'logoutLog/addLogoutLog');
            window.location.href = MENUCORE_SERVICE_URL + 'indexUser/logout';
          },
          queryFossMsg(rs){
            this.axios.post(FOSS_SERVER_NAME + "bse-baseinfo-web/common/queryMsgTotal.action")
            .then((res)=>{
              let result = res.data;
              // this.refreshFossMsg();
              // debugger;
              this.queryThisTimeOmsOrder(1);
              this.bigcss(rs,"vb");
              if(result){
            	  var msgVo = result.msgVo;
	            	if(msgVo != null ){
		            	var netMsgList = msgVo.netMsgList;
		            	if (netMsgList == null || netMsgList.length == 0 || netMsgList[0].noDealNum == 0) {
		            		//cleanMsg("受理在线通知");
		              } else {//受理在线通知
                    this.buildMsg("在线通知",FOSS_SERVER_NAME+"bse-querying-web/querying/acceptNetMsgOnline.action",netMsgList[0].noDealNum);
                    this.$emit('get-BuildMsg',this.buildMsgObj);
		              }
		            	var toDoMsgList = msgVo.toDoMsgList;
		            	if (toDoMsgList == null || toDoMsgList.length == 0) {
//		            		cleanMsg("处理待办");
//		            		cleanMsg("转寄退回件工单");
//		            		cleanMsg("快递更改单受理");
//		            		cleanMsg("悟空-管理营业部出发运单");
		              } else {
		                for ( var int = 0; int < toDoMsgList.length; int++) {
		                	var businessType = toDoMsgList[int].businessType;
                      if("PKP_WB_LAB_PT"==businessType){//更改单标签重打 处理待办
                        this.buildMsg("更改单标签重打",FOSS_SERVER_NAME+"pkp-deliver-web/waybill/todoActionIndex.action",toDoMsgList[int].noDealNum);
                        this.$emit('get-BuildMsg',this.buildMsgObj);
                      }
                      if("RETURN_GOODS_TODO"==businessType){
                        this.buildMsg("转寄退回件工单",ECS_SERVER_NAME+"tak-business-web/waybill/returnGoodsIndex.action?isUap=true",toDoMsgList[int].noDealNum);
                        this.$emit('get-BuildMsg',this.buildMsgObj);
                      }
                      if("PKP_WB_ACCECPT_WAY"==businessType){
                        this.buildMsg("快递更改单受理",ECS_SERVER_NAME+"tak-business-web/waybill/expBillChangeSel.action?isUap=true",toDoMsgList[int].noDealNum);
                        this.$emit('get-BuildMsg',this.buildMsgObj);
                      }
                      if("ECS_PDA_WAYBILL"==businessType){//悟空-管理营业部出发运单
                        this.buildMsg("快递待补录运单",ECS_SERVER_NAME+"tak-business-web/waybill/queryWaybill.action?isUap=true",toDoMsgList[int].noDealNum);
                        this.$emit('get-BuildMsg',this.buildMsgObj);
                      }
							    }
		            }
		            var normalMsgList = result.msgVo.normalMsgList;
		            if (normalMsgList == null || normalMsgList.length == 0 || normalMsgList[0].noDealNum == 0) {
		          		  //cleanMsg("供应商补录失败消息");
		          		  //cleanMsg("站内消息");
		            } else {
		              for ( var int = 0; int < normalMsgList.length; int++) {
		              	var msgType = normalMsgList[int].msgType;
                    if("SUPPLIER_FAIL_PATCH"==msgType){
                      this.buildMsg("供应商补录失败消息",FOSS_SERVER_NAME+"bse-baseinfo-web/common/instationMsgInit.action",normalMsgList[int].noDealNum);
                      this.$emit('get-BuildMsg',this.buildMsgObj);
                    }

                    if("NORMAL"==msgType){
                      this.buildMsg("普通消息",FOSS_SERVER_NAME+"bse-baseinfo-web/common/instationMsgInit.action",normalMsgList[int].noDealNum);
                      this.$emit('get-BuildMsg',this.buildMsgObj);
                    }
							    }
               }
//		                var noDealTotal = msgVo.noDealtotal;
//		                var collectionMsgList = msgVo.collectionMsgList;
//		                var autoAbandMsgList = msgVo.autoAbandMsgList;
//		                var callCenterWaybillMsgList = msgVo.callCenterWaybillMsgList;
//		                var noDealTotal = msgVo.noDealtotal;
//		                var collectionMsgList = msgVo.collectionMsgList;
//		                var failingInvoiceMsgList = msgVo.failingInvoiceMsgList;
//		                var unReconciliationMsgList = msgVo.unReconciliationMsgList;
//		                var noRepaymentMsgList = msgVo.noRepaymentMsgList;
//		                var debitWillOverList = msgVo.debitWillOverList;
//		                var deliveryReturnList = msgVo.deliveryReturnList;
                }else{

                }
              }else{
                  console.log("FOSS权限不足");
              }
            })
            .catch();
          },
          queryThisTimeOmsOrder(str){
            this.axios.post(OMS_SERVER_NAME + "oms-ltl-web/uaporder/queryOrder.action",
            {"uapOrderVO.uapOrderCondition.deptCode":userInfo.deptCode})
            .then((data)=>{
              $(".need-to-do").removeClass("open-always");
              //open-always
              if(data && data.uapOrderVO){
                  if(str==1){

                      var omsnum = data.uapOrderVO.currentWaitOrderNumber;

                      if(omsnum>0){

                          // $(".need-to-do").addClass("open-always");

                          var omsUrl = OMS_SERVER_NAME + 'oms-ltl-web/orderaccepted/accepted.action';
                          var contentfianl = "<li class=\'"+"1008"+"\'><p style=\"color: red;\" onclick=\"viewTab('零担订单受理',\'" + omsUrl + "\','DIV')\">"+"零担订单待受理"+"</p><span style=\"color: red;\">"+omsnum+"</span></li>";

                          // $(".messages-menu-xx").prepend(contentfianl);
                          this.buildMsgObj.messagesMenuXx.push(contentfianl);
                          if(document.hidden || messageflag) {
                              orderWebNotification(omsnum,'零担订单受理',omsUrl,'DIV');
                          };

                      }else{
                          // $(".need-to-do").removeClass("open-always");
                          // $('.messages-menu-xx .1008').remove();
                          var sumomsnum = sumTotalDBnum();
                          $("#messages-count").text(controlLengthrep(sumomsnum,2));
                          $(".need-to-do .messages-menu-header").html("<div>您有" + "<span></span>" + "新待办任务</div>");
                      }
                  }
                  if(str==2){
                      if(omsnum>0){
                          $(".need-to-do").addClass("open-always");
                      }else{
                          $(".need-to-do").removeClass("open-always");
                      }
                  }

              }else{
                  console.log("oms权限不足");
              }
            })
            .catch();
          },
          refreshRedPoint(){
            // $xxRedPoint = $(".need-to-do .messages-menu-xx")[0].childElementCount;
            // $dbRedPoint = $(".need-read .db-menu-db")[0].childElementCount;
            // let $xxRedPoint = document.querySelector(".need-to-do .messages-menu-xx").childElementCount;
            // let $dbRedPoint = document.querySelector(".need-read .db-menu-db").childElementCount;
            if($xxRedPoint == 0 || parseInt($xxRedPoint) == 0 ){
                // $('.need-to-do .message-reminding').hide();
                document.querySelector(".need-to-do .message-reminding").style.block = "none";
              }else{
                // $('.need-to-do .message-reminding').show();
                document.querySelector(".need-to-do .message-reminding").style.block = "block";
            }
            if($dbRedPoint == 0 || parseInt($dbRedPoint) == 0 ){
                // $('.need-read .message-reminding').hide();
                document.querySelector(".need-read .message-reminding").style.block = "none";
              }else{
                // $('.need-read .message-reminding').show();
                document.querySelector(".need-read .message-reminding").style.block = "block";
            }
          },
          smallcss(res,str) {
              var lis = "";
              var oms1009 = "";
              var cssshake = false;
              $(res.childNodes).each(
                  function (j, f) {
                      var connum = f.msgNumber;
                      var mesTitleName = queryMesTitle(f.mesCode);
                      if(''==mesTitleName){
                          mesTitleName = f.mesName ;
                      }
                      if(connum>0 && 'undefined'!=connum){
                          var umcslinkurl = f.linkUrl
                          var searchcss = '';
                          var paramumcs = 'DIV';
                          if(umcslinkurl.indexOf('?')!=-1){
                              var pairs=umcslinkurl.split('?');
                              searchcss = pairs[1]
                              paramumcs = getUrlParam(searchcss,"isdori");
                          }
                          if(paramumcs=='IFRAME'||'iframe'==paramumcs){
                              paramumcs = 'IFRAME';
                          }
                          if(f.mesCode == '1001'||f.mesCode == '1002'||f.mesCode == '1003'||f.mesCode == '1004'||f.mesCode == '1005'||f.mesCode == '2001'||f.mesCode == '2002'||f.mesCode == '2003'){
                          }else{
                              lis += "<li class=\'"+f.mesCode+"\'><p onclick=\"viewTab(\'" + mesTitleName + "\',\'" + f.linkUrl + "\',\'"+paramumcs+"\')\">"+f.mesName+"</p><span>"+f.msgNumber+"</span></li>";
                          }
                      }
                  });
            return lis;
          },
          buildMsg:function (title,url,number){
            this.$emit();
            if(title == '供应商补录失败消息'){
              var buildMsgStr = "<li class=\'"+title+"\'><p onclick=\"viewTab(\'"+"站内消息"+"\',\'" + url + "\','DIV')\">"+title+"</p><span>"+number+"</span></li>";
              // $(".db-menu-db").append(buildMsgStr)
              this.buildMsgObj.dbMenuDb.add(buildMsgStr);
            }else if(title == '普通消息'){
              var buildMsgStr = "<li class=\'"+title+"\'><p onclick=\"viewTab(\'"+"站内消息"+"\',\'" + url + "\','DIV')\">"+title+"</p><span>"+number+"</span></li>";
              // $(".db-menu-db").append(buildMsgStr)
              this.buildMsgObj.dbMenuDb.add(buildMsgStr);
            }else if(title == '快递待补录运单'){
              var buildMsgStr = "<li class=\'"+title+"\'><p onclick=\"viewTab(\'"+"悟空-管理营业部出发运单"+"\',\'" + url + "\','DIV')\">"+title+"</p><span>"+number+"</span></li>";
              // $(".messages-menu-xx").append(buildMsgStr);
              this.buildMsgObj.messagesMenuXx.add(buildMsgStr);
            }else if(title == '在线通知'){
              var buildMsgStr = "<li class=\'"+title+"\'><p onclick=\"viewTab(\'"+"受理在线通知"+"\',\'" + url + "\','DIV')\">"+title+"</p><span>"+number+"</span></li>";
              // $(".messages-menu-xx").append(buildMsgStr);
              this.buildMsgObj.messagesMenuXx.add(buildMsgStr);
            }else if(title == '更改单标签重打'){
              var buildMsgStr = "<li class=\'"+title+"\'><p onclick=\"viewTab(\'"+"代办事项"+"\',\'" + url + "\','DIV')\">"+title+"</p><span>"+number+"</span></li>";
              // $(".messages-menu-xx").append(buildMsgStr);
              this.buildMsgObj.messagesMenuXx.add(buildMsgStr);
            }else{
              var buildMsgStr = "<li class=\'"+title+"\'><p onclick=\"viewTab(\'"+title+"\',\'" + url + "\','DIV')\">"+title+"</p><span>"+number+"</span></li>";
              // $(".messages-menu-xx").append(buildMsgStr);
              this.buildMsgObj.messagesMenuXx.add(buildMsgStr);
            }
          },
          xxRedPoint(payload){
            console.log('xxRedPoint----',payload.$xxRedPoint);
          },
          dbRedPoint(payloade){
            console.log('dbRedPoint',payload.$dbRedPoint);
          },
          openSet(){
            this.openF = !this.openF
          },
          checkedTab(){
            document.querySelector('#sys-set-win').style.display = 'block';
          },
          showswitchdept(){
            document.querySelector('#switch-dept-win').style.display = 'block';
            this.GET_currentDeptInfo()
          }
        },
        components:{
          DropdownMenuCom:DropdownMenuCom
        }
    }
</script>

<style scoped>
  .messages-menu a:hover .message-tip{
    display: block;
  }
</style>
